/*
create by allen 2017/9/1
 */

const merchantFacade = require('../facade/merchant.server.facade');
const multiParty = require('connect-multiparty');
const uuid4 = require('uuid/v4');
const fileUtil = require('../utils/file.util');
const config = require('../config/env/config_development.json');
const  jwt = require('jsonwebtoken');
const _ = require('lodash');

module.exports = {
    register:register,
    login:login,
    reRegister:reRegister,
    getMerchantInfo:getMerchantInfo,
    getMerchantStatus:getMerchantStatus,
    logout,logout
};

function register(req, res, next) {
    let body = req.body;
    console.log(body);
    let idCard = req.files.file.idCard;
    let avator = req.files.file.avator;
    // console.log(req.files['null']);

    var destPath ="idcard/"+fileUtil.getName(idCard);
    fileUtil.readFile(idCard.path,function (error,data) {
        fileUtil.writeFile( config.file.path+destPath,data,function(error){
            if(error){
                return next(new Error(error))
            }
        })
    })
    var avatarDest = "avator/"+fileUtil.getName(avator);
    let merchant = body;
    merchant.idCard = destPath;
    merchant.avator = avatarDest;
    merchant.mStatus = "audit";
    fileUtil.readFile(avator.path,function (error,data) {
        fileUtil.writeFile(config.file.path+avatarDest,data,function(error){
            if(error){
                return next(new Error(error))
            }
            merchantFacade.addMerchant(merchant,function(error, result){
                if(error){
                    return next(new Error(error));
                }else {
                    return res.json({
                        code:result.ok
                    });
                }
            })
        })
    })




}

function login(req,res,next){
    let user = req.body;
    merchantFacade.findMerchantByName(user.name,function(error,result){
        if (error){
            return next(new Error(error));
        }
        if (_.isEmpty(result)){
           res.json({
                code:-1,
                message:"user no register"
            })
            return;
        }
        if (user.password==result.password){
            var merchant = {
                id:result._id,
                name:user.name
            }
            var token = jwt.sign(merchant,config.secret,{ expiresIn : "7d" });
            res.json({
                code:1,
                merchantId:result._id,
                token:token
            })
        }else{
            res.json({
                code:-1,
                message:"wrong password"
            });
        }

    })
}

function reRegister(req,res,next){

    let body = req.body;
    console.log(body);
    let idCard = req.files.file.idCard;
    let avator = req.files.file.avator;
    var destPath ="idcard/"+fileUtil.getName(idCard);
    fileUtil.readFile(idCard.path,function (error,data) {
        fileUtil.writeFile( config.file.path+destPath,data,function(error){
            if(error){
                return next(new Error(error))
            }
        })
    })
    var avatarDest = "avator/"+fileUtil.getName(avator);
    let merchant = body;
    merchant.name = req.name;
    merchant.idCard = destPath;
    merchant.avator = avatarDest;
    merchant.mStatus = "audit";
    fileUtil.readFile(avator.path,function (error,data) {
        fileUtil.writeFile(config.file.path+avatarDest,data,function(error){
            if(error){
                return next(new Error(error))
            }
            merchantFacade.replaceMerchant(merchant,function(error, result){
                if(error){
                    return next(new Error(error));
                }else {
                    return res.json({
                        code:1
                    });
                }
            })
        })
    })

}

function getMerchantInfo(req,res,next){
    var name = req.name;
    merchantFacade.findMerchantByName(name,function (error,result) {
        if (error){
            return next(new Error(error));
        }else{
            if (_.isEmpty(result)){
                res.json({});
            }
            result.idCard = config.file.apiPath+result.idCard;
            result.avator = config.file.apiPath+result.avator;
           res.json(result);
        }
    })
}

function getMerchantStatus(req,res,next){
    var merchantId = req.merchantId;
    merchantFacade.findMerchantStatusById(merchantId,function (error,result) {
        if (error){
            return next(new Error(error));
        }else{
                res.json({
                    status:result.mStatus
                });
        }
    })
}

function logout(req,res,next){
    res.json({
        code:1
    })
}